package jjn.carl.linkedlist;

import commons.ListNode;

/**
 * @author Jiang Jining
 * @since 2023-07-02 21:51
 */
public class LeetCode206 {
    public ListNode reverseList(ListNode head) {
        ListNode current = head, previous = null;
        while (current != null) {
            // 必须使用temp存储一下current的下一个节点，防止找不到
            ListNode temp = current.next;
            // 当前节点指向掉头
            current.next = previous;
            // 移动前一个节点
            previous = current;
            current = temp;
        }
        return previous;
    }
}
